$(function(){
        get_current_time()
        var items_in_cart=get_cart_list()
        show_local_count();
        show_cart_list(items_in_cart);
        show_save_list();
        show_amount(items_in_cart)
        show_total_save(items_in_cart)
        pay_money()
})
function show_page_element(place,value){
    $(place).html(value)
}
function get_total_count(){
    return localStorage.getItem("item_total_count")||0;
}
function get_cart_list(){
    return JSON.parse(localStorage.getItem("items_in_cart"))
}
function show_local_count(){
    var total_count=get_total_count()
    show_page_element("#goods_count",total_count)
}
function show_cart_list(items_in_cart){
    $('#model').tmpl(items_in_cart).appendTo('#table_head');
}
function show_save_list(){
    var save_list = get_save_list();
    $('#promotion_model').tmpl(save_list).appendTo('#free_table_head');
}
function get_save_list(){
    var items_in_cart=get_cart_list();
    var save_list = _.filter(items_in_cart,function(item){
        return  item.free_count>0
    })
    return save_list
}
function get_amount(items_in_cart){
    var amount=0;
    _.each(items_in_cart,function(item){
        amount += item.Price*item.count
    })
    return amount.toFixed(2)
}
function get_save(items_in_cart){
    var save=0;
    _.each(items_in_cart,function(item){
        save += item.free_count*item.Price
    })
    return save.toFixed(2)
}
function show_amount(items_in_cart){
    var amount=get_amount(items_in_cart)
    show_page_element("#amount",amount)
}
function show_total_save(items_in_cart){
    var save=get_save(items_in_cart)
    show_page_element("#save",save);
}
function pay_money(){
    $("#pay").click(function (){
        localStorage.clear();
    })
}
function dateDigitToString(num) {
    return num < 10 ? '0' + num : num;
}
function get_current_time(){
    var currentDate = new Date();
    year = dateDigitToString(currentDate.getFullYear());
    month = dateDigitToString(currentDate.getMonth() + 1);
    date = dateDigitToString(currentDate.getDate());
    hour = dateDigitToString(currentDate.getHours());
    minute = dateDigitToString(currentDate.getMinutes());
    second = dateDigitToString(currentDate.getSeconds());
    var formattedDateString = year + '年' + month + '月' + date + '日 ' + hour + ':' + minute + ':' + second;
    show_page_element("#print_time",formattedDateString);
    return formattedDateString;
}
